package controllers.listeners.lexicon;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import controllers.shared.GetInputLoop;

import objects.Form;
import objects.Lexicon;
import objects.LexiconList;
import objects.Stem;

import views.ViewLexicon;

import models.LexiconModel;

/* Adds a new form for the current label / syntactic category */
/* actionPerformed when you click the "Add Form" button */
public class AddFormActionListener implements ActionListener {
	
	private ViewLexicon view;
	
	public AddFormActionListener() {
		view = null;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Lexicon lexicon = LexiconModel.getLexicon();
		String input = null;
		GetInputLoop.setInputDialog("Enter a new Form:", "New Form", JOptionPane.OK_CANCEL_OPTION);
		GetInputLoop.setErrorDialog("Please enter a name for the new form:", "Error");
		
		input = GetInputLoop.getInput();
		
		if (input == null) {
			return;
		}
		
		for (LexiconList currentLexiconList : lexicon.getLexiconList()) {
			if (view.getSelectedCategory().equals(currentLexiconList.getLabel())) {
				currentLexiconList.addFormListItem(input);
				
				for (Stem currentStem : currentLexiconList.getStemList()) {
					Form form = new Form();
					form.setName(input);
					form.setValue("");
					currentStem.getFormList().add(form);
				}
				
				view.refreshTable();
				break;
			}
		}
	}
	
	public void setView(ViewLexicon view) {
		this.view = view;
	}
}
